let _option = document.querySelector(".option_box");
let option_box = document.querySelectorAll(".option_box option");
// console.log(option_box);
let tow = document.querySelector(".tow")
let three = document.querySelector(".three")
let four = document.querySelector(".four")
let layui = document.querySelector("layui");
let day_time = [];
let value;
var res;


if (sessionStorage.getItem("token")) {

    _option.addEventListener("change", function () {
        tow.innerHTML = ""
        three.innerHTML = ""
        four.innerHTML = ""
        ajax("http://newshopapi.0melon0.cn/api/f_user/chooseday", {
            "token": `${sessionStorage.getItem("token")}`,
            "day_time": _option.value,

        }, "GET").then(re => {

            res = re;
            run();
        })

    })

    ajax("http://newshopapi.0melon0.cn/api/f_user/chooselist", {

        "token": `${sessionStorage.getItem("token")}`
    }, "GET").then(res1 => {
        res1.result.list.forEach(function (item) {
            // _time.push(item.day_time)
            // console.log( item.day_time);
            _option.innerHTML += `<option value="${item.day_time}" id="${item.day_time}">${item.day_time}</option>`;
            day_time.push(item.day_time)


        })
        ajax("http://newshopapi.0melon0.cn/api/f_user/chooseday", {
            "token": `${sessionStorage.getItem("token")}`,
            "day_time": res1.result.list[0].day_time,
        }, "GET").then(re => {
            console.log(re);
            res = re;
            run();

        });

    });
    let _tow = document.querySelector(".tow>span")
    let _three = document.querySelector(".three")
    let _four = document.querySelector(".four")

};

function run() {
    tow.innerHTML += `<span id="${res.result.list[0].show_id}">${res.result.list[0].show_name}</span>`;
    three.innerHTML += `<span id="${res.result.list[1].show_id}">${res.result.list[1].show_name}</span>`;
    four.innerHTML += `<span id="${res.result.list[2].show_id}">${res.result.list[2].show_name}</span>`;

    let timestamp = Date.now();
    let sessiontime0 = _option.value + " " + res.result.list[0].show_name.split("-")[0]
    let sessiontime1 = _option.value + " " + res.result.list[1].show_name.split("-")[0]
    let sessiontime2 = _option.value + " " + res.result.list[2].show_name.split("-")[0]

    let cctime0 = new Date(`${sessiontime0}`).getTime()
    let cctime1 = new Date(`${sessiontime1}`).getTime()
    let cctime2 = new Date(`${sessiontime2}`).getTime()

    if (cctime0 > timestamp) {
        tow.innerHTML += `<p>可预约</p>`
    } else {
        tow.innerHTML += `<p>不可预约</p>`

    }
    if (cctime1 > timestamp) {
        three.innerHTML += `<p>可预约</p>`
    } else {
        three.innerHTML += `<p>不可预约</p>`

    }
    if (cctime2 > timestamp) {
        four.innerHTML += `<p>可预约</p>`
    } else {
        four.innerHTML += `<p>不可预约</p>`

    }


    let _tow = document.querySelector(".tow>p")
    let _three = document.querySelector(".three>p")
    let _four = document.querySelector(".four>p")
    let _towspan = document.querySelector(".tow>span")
    let _threespan = document.querySelector(".three>span")
    let _fourspan = document.querySelector(".four>span")
   
    _tow.addEventListener("click", function () {
        if (_tow.innerHTML == "可预约") {
            sessionStorage.setItem("day_time", `${_option.value}`);
            sessionStorage.setItem("cc_time", `${_towspan.innerHTML}`);
            // console.log(_towspan.innerHTML);
            // console.log(_option.value);
            // console.log(_towspan.id);
            ajax("http://newshopapi.0melon0.cn/api/f_user/bookday", {
                "token": `${sessionStorage.getItem("token")}`,
                "day_time": `${_option.value}`,
                "show_id": `${_towspan.id}`,
            }, "POST").then(re1 => {
                ajax("http://newshopapi.0melon0.cn/api/f_user/Qrcode", {
                    "token": `${sessionStorage.getItem("token")}`,
                    "id": `${re1.result.id}`,
                }, "GET").then(re2 => {
                    console.log(re2);
                    sessionStorage.setItem("img", `${re2.data.data}`);
                    window.location.href = "./Appointment.html";
                })

            })

           
        }

    })
    _three.addEventListener("click", function () {
        if (_three.innerHTML == "可预约") {
            sessionStorage.setItem("day_time", `${_option.value}`);
            sessionStorage.setItem("cc_time", `${_threespan.innerHTML}`);
        //   console.log(123);
            ajax("http://newshopapi.0melon0.cn/api/f_user/bookday", {
                "token": `${sessionStorage.getItem("token")}`,
                "day_time": `${_option.value}`,
                "show_id": `${_threespan.id}`,
            }, "POST").then(re1 => {
                ajax("http://newshopapi.0melon0.cn/api/f_user/Qrcode", {
                    "token": `${sessionStorage.getItem("token")}`,
                    "id": `${re1.result.id}`,
                }, "GET").then(re2 => {
                    console.log(re2);
                    //  console.log(re2.data.data);
                    sessionStorage.setItem("img", `${re2.data.data}`);
                    window.location.href = "./Appointment.html";
                    // console.log(sessionStorage.getItem("img"));
    
                })

            })
        }
    })
    _four.addEventListener("click", function () {
        if (_four.innerHTML == "可预约") {
            sessionStorage.setItem("day_time", `${_option.value}`);
            sessionStorage.setItem("cc_time", `${_fourspan.innerHTML}`);
            // console.log(sessionStorage.getItem("day_time"));
            ajax("http://newshopapi.0melon0.cn/api/f_user/bookday", {
                "token": `${sessionStorage.getItem("token")}`,
                "day_time": `${_option.value}`,
                "show_id": `${_fourspan.id}`,
            }, "POST").then(re1 => {
                ajax("http://newshopapi.0melon0.cn/api/f_user/Qrcode", {
                    "token": `${sessionStorage.getItem("token")}`,
                    "id": `${re1.result.id}`,
                }, "GET").then(re2 => {
                    console.log(re2);
                    //  console.log(re2.data.data);
                    sessionStorage.setItem("img", `${re2.data.data}`);
                    window.location.href = "./Appointment.html";
                    // console.log(sessionStorage.getItem("img"));
    
                })

            })
        }
    })
}